﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using SBS_Map_Editor.MapClasses;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace SBS_Map_Editor
{
    public partial class MainForm : Form
    {
        #region Variables
        //ContentBuilder contentBuilder;
        ContentManager contentManager;

        public IntPtr ViewportHandle { get { return Viewport.Handle; } }

        //StreamReader sr;
        static string[] mapData = new string[10];

        // Properties
        public string[] MapData { get; set; }
        #endregion

        #region Constructor
        public MainForm()
        {
            InitializeComponent();
            //contentManager.RootDirectory = "Content";
            //contentBuilder = new ContentBuilder();
            contentManager = new ContentManager(modelViewerControl.Services);

            //this.Shown += OpenMapData;
        }
        #endregion

        #region File Functions
        private void New_Click(object sender, EventArgs e)
        {
            //Map.FirstRun = false;
            Game1.map = new Map();

        }

        void OpenMapData(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();

            // Default to the directory which contains our content files.
            string assemblyLocation = Assembly.GetExecutingAssembly().Location;
            string relativePath = Path.Combine(assemblyLocation, "../../../../data");
            string contentPath = Path.GetFullPath(relativePath);

            fileDialog.InitialDirectory = contentPath;

            fileDialog.Title = "Load Map Data";

            fileDialog.Filter = "Text Files (*.txt)|*.txt|" +
                                "All Files (*.*)|*.*";
            int i = 0;
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                StreamReader sr = new StreamReader(fileDialog.FileName);
                string line;
                while ((line = sr.ReadLine()) != null)
                {

                    //if (!sr.EndOfStream)
                    //{
                    mapData[i] = line;
                    i++;
                    //}
                }
                //LoadList(mapData);
            }
            LoadList(mapData);
        }

        private void Open_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            // Default to the directory which contains our content files.
            string assemblyLocation = Assembly.GetExecutingAssembly().Location;
            string relativePath = Path.Combine(assemblyLocation, "../../../../data/Map");
            string contentPath = Path.GetFullPath(relativePath);
            Game1.map.Path = contentPath;

            open.InitialDirectory = Game1.map.Path;

            open.Title = "Load Map";

            open.Filter = "Content Files (*.THM; *.zmx)|*.THM; *.zmx|" +
                                "Hannitizer Map Files (*.THM)|*.THM|" +
                                "ZombieSmashers Map Files (*.zmx)|*.zmx|" +
                                "All Files (*.*)|*.*";

            if (open.ShowDialog() == DialogResult.OK)
            {
                Game1.map.Read();
            }
        }

        private void Save_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            // Default to the directory which contains our content files.
            string assemblyLocation = Assembly.GetExecutingAssembly().Location;
            string relativePath = Path.Combine(assemblyLocation, "../../../../data/Map");
            string contentPath = Path.GetFullPath(relativePath);
            Game1.map.Path = contentPath;

            save.InitialDirectory = Game1.map.Path;
            save.Title = "Save Map";
            save.Filter = "Content Files (*.THM; *.zmx)|*.THM; *.zmx|" +
                                "Hannitizer Map Files (*.THM)|*.THM|" +
                                "ZombieSmashers Map Files (*.zmx)|*.zmx|" +
                                "All Files (*.*)|*.*";

            if (save.ShowDialog() == DialogResult.OK)
            {
                Game1.map.Write();
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            //OpenFileDialog open = new OpenFileDialog();
            //// Default to the directory which contains our content files.
            //string assemblyLocation = Assembly.GetExecutingAssembly().Location;
            //string relativePath = Path.Combine(assemblyLocation, "../../../../../Model");
            //string contentPath = Path.GetFullPath(relativePath);

            //open.InitialDirectory = contentPath;
            //open.Title = "Load Model";
            //open.Filter = "Model Files (*.fbx;*.x)|*.fbx;*.x|" +
            //                    "FBX Files (*.fbx)|*.fbx|" +
            //                    "X Files (*.x)|*.x|" +
            //                    "All Files (*.*)|*.*";

            //if (open.ShowDialog() == DialogResult.OK)
            //{
            //    LoadModel(open.FileName);
            //}

        }
        #endregion

        #region Loading Functions
        /// <summary>
        /// Loads a new 3D model file into the ModelViewerControl.
        /// </summary>
        public static void LoadModel(ContentManager content, Model[] model)
        {
            for (int i = 0; i < model.Length; i++)
            {
                model[i] = content.Load<Model>(@"Model\" + mapData[i].ToString());
            }
        }

        public void LoadList(string[] md)
        {
            pieceList.Update();
            for (int i = 0; i < md.Length; i++)
            {
                if (md[i] != null)
                    pieceList.Items.Add(md[i]);
            }
            pieceList.EndUpdate();
        }
        #endregion

        #region Drawing Modes
        private void editSelect_Click(object sender, EventArgs e)
        {
            Game1.DrawMode = DrawingMode.SegmentSelection;
        }

        private void editCol_Click(object sender, EventArgs e)
        {
            Game1.DrawMode = DrawingMode.CollisionMap;
        }

        private void editLedge_Click(object sender, EventArgs e)
        {
            Game1.DrawMode = DrawingMode.Ledge;
        }

        private void editScript_Click(object sender, EventArgs e)
        {
            Game1.DrawMode = DrawingMode.Script;
            Game1.EditMode = EditingMode.Script;
        }
        #endregion

        //private void listBox1_Click(object sender, EventArgs e)
        //{
        //    LoadModel(listBox1.SelectedItem.ToString());
        //}
    }
}